
PLUG.Platform = PLUG_PLATFORM_SERVER

PLUG.ClassName = "sv_chatcommands"
PLUG.PrintName = "Chat Commands Module"
PLUG.Author = "thomasfn"

local Prefix = "!"

function PLUG:AddHooks()
	plugs.Hook( PLUG_PLATFORM_SERVER, "PlayerSay" )
	
	self:AddChatCommand( "kick", "PROCESS", "kick", "[FINDTARGET] [REMAINDER]" )
	self:AddChatCommand( "ban", "PROCESS", "ban", "[FINDTARGET] [ARG]" )
	self:AddChatCommand( "slay", "PROCESS", "slay", "[FINDTARGET]" )
	self:AddChatCommand( "setgroup", "PROCESS", "setgrp", "[FINDTARGET] [ARG]" )
	self:AddChatCommand( "givepriv", "PROCESS", "givepriv", "[FINDTARGET] [ARG]" )
	self:AddChatCommand( "takepriv", "PROCESS", "takepriv", "[FINDTARGET] [ARG]" )
	self:AddChatCommand( "motd", "PROCESS", "motd_see" )
end

function PLUG:PlayerSay( ply, text, public )
	if (string.Left( text, 1 ) == Prefix) then
		local remaining = string.sub( text, 2 )
		local args = string.Explode( " ", remaining )
		local com = string.lower( args[1] )
		if (!com) then return end
		table.remove( args, 1 )
		local result = self:RunChatCommand( ply, com, args )
		if (result) then return "" end
	end
end

local coms = {}

function PLUG:AddChatCommand( ... )
	table.insert( coms, { ... } )
end

function PLUG:RunChatCommand( ply, com, args )
	for _, tab in pairs( coms ) do
		if (com == tab[1]) then
			ply:Log( "chatcommands", "Ran " .. com .. " with args: " .. table.concat( args, "," ) )
			if (tab[2] == "PROCESS") then
				self:ProcessChatCommand( ply, tab[3], args, tab[4] )
			else
				plugs.Call( tab[2], ply, com, args )
			end
			return true
		end
	end
end

function PLUG:ProcessChatCommand( ply, com, args, pattern )
	local p = string.Explode( " ", pattern )
	local cnt
	local newargs = {}
	for cnt=1, #p do
		local i = p[ cnt ]
		local a = args[ cnt ]
		newargs[ cnt ] = i
		if (i == "[FINDTARGET]") then
			local tgs = self:FindTargets( a )
			if (#tgs == 0) then
				ply:PrintMessage( HUD_PRINTTALK, "No players found with that name!" )
				return
			end
			if (#tgs > 1) then
				ply:PrintMessage( HUD_PRINTTALK, "More than 1 player found with that name!" )
				return
			end
			newargs[ cnt ] = tgs[1]:GetGID()
		end
		if (i == "[ARG]") then
			newargs[ cnt ] = a
		end
		if (string.Left( i, 6 ) == "[ARG]:") then
			newargs[ cnt ] = args[ tonumber( string.sub( i, 7 ) or "1" ) ]
		end
		if (i == "[REMAINDER]") then
			local temp = ""
			local c = 0
			for c=cnt, #args do
				temp = temp .. " " .. args[ c ]
			end
			newargs[ cnt ] = string.sub( temp, 2 )
		end
		// print( newargs[ cnt ] )
	end
	if (!plugs.Call( "DoConCommand", ply, com, newargs )) then
		ply:PrintMessage( HUD_PRINTTALK, "Command failed!" )
	end
end

function PLUG:FindTargets( name )
	if (!name) then return {} end
	local found = {}
	for _, ply in pairs( player.GetAll() ) do
		if (string.find( string.lower( ply:Nick() ), string.lower( name ) )) then table.insert( found, ply ) end
	end
	return found
end

// OBSOLETE
function PLUG:DoChatToConCommand( ply, com, args, id )
	local tgs = self:FindTargets( args[1] )
	if (#tgs == 0) then
		ply:PrintMessage( HUD_PRINTTALK, "No players found with that name!" )
		return
	end
	if (#tgs > 1) then
		ply:PrintMessage( HUD_PRINTTALK, "More than 1 player found with that name!" )
		return
	end
	local t = tgs[1]
	if ((!t) || (!t:IsValid())) then
		ply:PrintMessage( HUD_PRINTTALK, "Target invalid!" )
		return
	end
	if (!plugs.Call( "DoConCommand", ply, id, { t:GetGID(), args[2] } )) then
		ply:PrintMessage( HUD_PRINTTALK, "You do not have enough privileges to do that!" )
	end
end